//
//  COHotMainController.swift
//  CocoProject
//
//  Created by Vill on 2023/11/7.
//

import UIKit
import SPPageMenu

class COHotMainController: COBaseController,SPPageMenuDelegate {
    
    var subControllers = [UIViewController]()
    var tabArray = [String]()
    var pageMenuView = SPPageMenu()
    
    lazy var mainView: UIScrollView = {
        let res = UIScrollView.init(frame: CGRect(x: 0, y:KStatusAndNavBarHeight, width: SCREENWIDTH, height: SCREENHEIGHT - CGRectGetMaxY(self.pageMenuView.frame) - KTabBarAndSafeHeight))
        res.backgroundColor = .clear
        res.isPagingEnabled = true
        res.showsHorizontalScrollIndicator = false
        res.bounces = false
        return res
    }()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        kLiveCalling.keySaveToNSDefaults(with: false)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tabArray = ["Hot","New","Follow"]
        
        let pageMenu =  SPPageMenu(frame: CGRectMake(0,KStatusAndNavBarHeight - PTW(44), SCREENWIDTH - PTW(100), PTW(44)), trackerStyle: .lineAttachment)
        pageMenu.permutationWay = .notScrollEqualWidths
//        pageMenu.spacing = PTW(5)
        pageMenu.tracker.isHidden = true
        pageMenu.contentInset = UIEdgeInsets(top: 0, left: PTW(5), bottom: PTW(5), right: 0)
        pageMenu.backgroundColor = .clear
        pageMenu.itemTitleFont = UIFont.pingfangFont(ofSize: PTW(18), weight: .semibold)
        pageMenu.selectedItemTitleFont = UIFont.pingfangFont(ofSize: PTW(25), weight: .semibold)
        pageMenu.unSelectedItemTitleColor = UIColor.colorWithHex("#FFFFFF", 0.5)
        pageMenu.selectedItemTitleColor = UIColor.colorWithHex("#FFFFFF", 1)
        pageMenu.dividingLine.isHidden = true
        pageMenu.delegate = self
        pageMenu.selectedItemIndex = 0
        pageMenu.bridgeScrollView = self.mainView
        self.pageMenuView = pageMenu
            
        self.view.addSubview(self.pageMenuView)
        self.view.addSubview(self.mainView)
        self.pageMenuView.setItems(self.tabArray, selectedItemIndex: 0)
        self.mainView.contentSize = CGSizeMake(SCREENWIDTH * CGFloat(self.tabArray.count), self.mainView.frame.size.height)
        for i in 0..<self.tabArray.count {
            let vc = COHotListController()
            vc.chooseType = i
            vc.countryCode = self.getCountryCodeJson()
            self.addChild(vc)
            self.subControllers.append(vc)
        }
        if(self.pageMenuView.selectedItemIndex < self.subControllers.count){
            let baseVc = self.subControllers[self.pageMenuView.selectedItemIndex]
            self.mainView.addSubview(baseVc.view)
            baseVc.view.frame = CGRectMake(SCREENWIDTH * CGFloat(self.pageMenuView.selectedItemIndex), 0, SCREENWIDTH, SCREENHEIGHT - CGRectGetMaxY(self.pageMenuView.frame) - KTabBarAndSafeHeight)
            self.mainView.contentOffset = CGPoint(x: SCREENWIDTH * CGFloat(self.pageMenuView.selectedItemIndex), y: 0)
            self.mainView.contentSize = CGSize(width: self.tabArray.count * Int(SCREENWIDTH), height: 0)
        }
    }
    
    func pageMenu(_ pageMenu: SPPageMenu, itemSelectedFrom fromIndex: Int, to toIndex: Int) {
        if(labs(toIndex - fromIndex) >= 2){
            self.mainView.setContentOffset(CGPointMake(SCREENWIDTH * CGFloat(toIndex), 0), animated: false)
        }else{
            self.mainView.setContentOffset(CGPointMake(SCREENWIDTH * CGFloat(toIndex), 0), animated: true)
        }
        if (self.subControllers.count <= toIndex){return}
        let targetViewController = self.subControllers[toIndex]
        targetViewController.view.frame = CGRectMake(SCREENWIDTH * CGFloat(toIndex), 0, SCREENWIDTH, SCREENHEIGHT - CGRectGetMaxY(self.pageMenuView.frame) - KTabBarAndSafeHeight)
        self.mainView.addSubview(targetViewController.view)
    }
    
    // 获取时区国家
    func getCountryCodeJson() -> [CountryCodeModel] {
        var countryCodeArr = [CountryCodeModel]()     // 国家代码数组
        let path = Bundle.main.path(forResource: "COHotListJson", ofType: "geojson")
        let url = URL(fileURLWithPath: path!)
        do {
            let data = try Data(contentsOf: url)
            let jsonData:Any = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.mutableContainers)
            let jsonArr = jsonData as! NSArray
            if let countrycodes = [CountryCodeModel].deserialize(from: jsonArr) {
                for countrycode in countrycodes.compactMap({ return $0 }) {
                    countryCodeArr.append(countrycode)
                }
            }
        } catch let error as Error? {
            print("读取本地数据出现错误!",error as Any)
        }
        return countryCodeArr
    }
}
